<?php
// 应用公共文件


use app\common\Cache;
use app\common\enum\Cache as CacheEnum;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;
use think\facade\Request;

if (!function_exists('uuid')) {

    /**
     * UUID
     */
    function uuid(): string
    {
        $str = sprintf(
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff)
        );

        $sysSalt = Config::get('encrypt.sys_salt');
        if (empty($sysSalt)) {
            $sysSalt = 'jon-cc';
        }

        $agent = Request::header('user-agent');
        if (!empty($agent)) {
            $str .= '-' . md5($agent . $sysSalt);
        }
        return $str;
    }
}

if (!function_exists('get_mca')) {

    /**
     * module controller action
     */
    function get_mca(): array
    {
        return [app('http')->getName(), app()->request->controller(), app()->request->action()];
    }
}

if (!function_exists('deny')) {

    /**
     * 权限判断
     */
    function deny(string $needle, array $haystack): bool
    {
        if (empty($haystack)) {
            return true;
        }
        if ('*' === $haystack[0]) {
            return false;
        }
        if (in_array($needle, $haystack)) {
            return false;
        }

        return true;
    }
}

if (!function_exists('operate_log')) {

    /**
     * 操作日志
     */
    function operate_log(string $type, string $table, int $id = 0): void
    {
        try {
            $uid = Cache::get(CacheEnum::UID);
            if (empty($uid)) {
                return;
            }

            $request = request();
            if (empty($id)) {
                if (!empty($request->param('id'))) {
                    $id = $request->param('id');
                } elseif (!empty($request->param('ids'))) {
                    $id = $request->param('ids');
                }
            }

            $app = app();
            [$module, $controller, $action] = get_mca();
            $runtime = number_format(microtime(true) - $app->getBeginTime(), 10, '.', '');
            $mem = number_format((memory_get_usage() - $app->getBeginMem()) / 1024, 2);

            $data = [
                'user_id' => $uid,
                'type' => $type,
                'table' => $table,
                'data_id' => $id,
                'token' => Cache::get(CacheEnum::TOKEN),
                'controller' => $controller,
                'action' => $action,
                'runtime' => number_format((float)$runtime, 6),
                'memory' => $mem,
                'url' => $request->url(true),
                'method' => $request->method(),
                'params' => json_encode($request->param(), JSON_UNESCAPED_UNICODE),
                'content' => $request->getContent(),
            ];

            $sql = 'insert into sys_user_log_operate set ';
            foreach ($data as $column => $v) {
                $sql .= " `$column` = '{$v}',";
            }

            $sql = rtrim($sql, ',');
            Db::execute($sql);
        } catch (Exception $e) {
            Log::record($e->getMessage());
        }
    }
}

if (!function_exists('p')) {

    /**
     * 获取参数快捷方式
     */
    function p(string $name = ''): mixed
    {
        $p = request()->bizParams;
        if (!empty($name)) {
            return $p[$name] ?? null;
        }

        return $p;
    }
}